<?php
class Worklog_m extends CI_Model{
	
	public $user_name = '';
	function __construct(){//重载父类的数据库连接方法
		parent::__construct();
		$this->load->helper('string');
		$this->user_name = $this->session->userdata('user_name');
		$this->user_id = $this->session->userdata('user_id');
		$this->dept_name = $this->session->userdata('dept_name');
		
	}

	//检查指定日期是否有数据
	function worklog_check($get_date,$row = false){
		$query = $this->db->where('log_date', date('Y-m-d',$get_date))->where('user_id', $this->user_id)->get('worklog');
		if($row){
			return $query->row();
		}else{
			return $query->num_rows();
		}
	}
	//
	function worklog_new($data){	

		if(empty($data['id'])){			
			if($this->worklog_check($data['log_date']) > 0){
				return false;
			}else{
				$week = date('w',$data['log_date']);
				$weekarray=array("星期日","星期一","星期二","星期三","星期四","星期五","星期六"); 
				$array = array(
					'id' => '',
					'user_id' => $this->user_id,
					'user_name' => $this->user_name,
					'log_am' => htmlspecialchars($data['log_am'], ENT_QUOTES), 
					'log_pm' => htmlspecialchars($data['log_pm'], ENT_QUOTES),
					'log_morn' => htmlspecialchars($data['log_morn'], ENT_QUOTES),
					'log_date' => date('Y-m-d',$data['log_date']),
					'log_w' => $weekarray[$week]
				);
				return $this->db->insert('worklog', $array); 
			}
		}else{
			return $this->db->update('worklog', array('log_am' => $data['log_am'],'log_pm'=>$data['log_pm'],'log_morn'=>$data['log_morn']), array('id' => $data['id'],'user_id'=>$this->user_id));
			//echo $this->db->affected_rows();
			//return $this->db->affected_rows() == 1;
		}
	}
	function worklog_is_b($data){
		$arr = explode("-",$data);
		if(end($arr)<10){
			$str = reset($arr).'-'.'0'.end($arr);
		}else{
			$str = $data;
		}
		$query = $this->db->select('log_date')->like('log_date', $str)->where('user_id', $this->user_id)->get('worklog');
		//echo $this->db->last_query();//输出随后一次查询的语句
		return $query->result_array();
		
	}
	/*
	function worklog_all($searchParams = ''){
		
		$this->db->where('user_id', $this->user_id); 
		if($searchParams != ''){
			if(@$searchParams['wids'])
				$this->db->where('log_date >=', $searchParams['wids']);
			if(@$searchParams['wide'])
				$this->db->where('log_date <=', $searchParams['wide']);
			if(@$searchParams['key']){
				$this->db->like('log_am', $searchParams['key']); 
				$this->db->or_like('log_pm', $searchParams['key']); 
				$this->db->or_like('log_morn', $searchParams['key']); 
			}

		}
		$query=$this->db->get('worklog');
		return $query->num_rows();
		

	}
	function worklog_limit($start,$end,$sort,$order,$searchParams = ''){	
		
		$this->db->where('user_id', $this->user_id); 
		if($searchParams != ''){
			if(@$searchParams['wids'])
				$this->db->where('log_date >=', $searchParams['wids']);
			if(@$searchParams['wide'])
				$this->db->where('log_date <=', $searchParams['wide']);
			if(@$searchParams['key']){
				$this->db->like('log_am', $searchParams['key']); 
				$this->db->or_like('log_pm', $searchParams['key']); 
				$this->db->or_like('log_morn', $searchParams['key']); 
			}

		}		
		$this->db->order_by("$sort", $order); 
		$this->db->limit($end,$start);
		$query=$this->db->get('worklog');
		//echo $this->db->last_query();//输出随后一次查询的语句
		return $query->result();
		
	}
	*/
	function getlistdata($data){
		$this->db->from('worklog');
		$this->db->where('user_id', $this->user_id); 
		
		if(array_key_exists('wids',$data))
			$this->db->where('log_date >=', $data['wids']);
		if(array_key_exists('wide',$data))
			$this->db->where('log_date <=', $data['wide']);
		if(array_key_exists('key',$data)){
			$where = "(log_am like '".$data['key']."' AND log_pm='".$data['key']."' OR log_morn='".$data['key']."')";
			$this->db->where($where);
		}
		//在order、group或limit前查询总数
        $db = clone($this->db);
        $total = $this->db->count_all_results();
		
		$this->db = $db;
		$this->db->order_by('log_date','desc'); 
		$start=($data['page']-1)*$data['rows'];
		$this->db->limit($data['rows'],$start);
		$query = $this->db->get();
		//echo $this->db->last_query();//输出随后一次查询的语
		$data = $query->result_array();
		return array('data'=>$data, 'total'=>$total);
	}
	
	function getmanagelistdata($data){

		if(!empty($data['user_id']) || !empty($data['role_id']) || !empty($data['wids']) || !empty($data['wide'])){
			$this->db->select('dept.dept_name,role.role_name,worklog.user_id,worklog.user_name,worklog.log_date,worklog.log_w,worklog.log_am,worklog.log_pm,worklog.log_morn,worklog.id');
			$this->db->from('worklog');
			$this->db->join('users', 'worklog.user_id = users.user_id', 'left');
			$this->db->join('role', 'role.role_id = users.role_id', 'left');
			$this->db->join('dept', 'dept.dept_id = users.dept_id', 'left');
			
			if(array_key_exists("user_id", $data)){
				$user_arr = explode(',',reduce_multiples($data['user_id'],',',true));
				$this->db->where_in('worklog.user_id', $user_arr);
			}
			if(array_key_exists("role_id",$data))
				$this->db->where('role.role_id', $data['role_id']);
			if(array_key_exists("wids",$data))
				$this->db->where('log_date >=', $data['wids']);
			if(array_key_exists("wide",$data)){
				$this->db->where('log_date <=', $data['wide']);
			}
			//在order、group或limit前查询总数
			$db = clone($this->db);
			$total = $this->db->count_all_results();
			$this->db = $db;
			$this->db->order_by('dept.dept_id asc, role.role_id asc, worklog.user_id asc'); 
			$start=($data['page']-1)*$data['rows'];
			$this->db->limit($data['rows'],$start);
			$query = $this->db->get();
			//echo $this->db->last_query();//输出随后一次查询的语
			$data = $query->result_array();
			return array('data'=>$data, 'total'=>$total);
			
		}

	}
	/*
	function worklog_manage_all($searchParams = ''){
		if($searchParams != ''){
			$this->db->select('dept.dept_name,role.role_name,worklog.user_id,worklog.user_name,worklog.log_date,worklog.log_w,worklog.log_am,worklog.log_pm,worklog.log_morn,worklog.id');
			$this->db->from('worklog');
			$this->db->join('users', 'worklog.user_id = users.user_id', 'left');
			$this->db->join('role', 'role.role_id = users.role_id', 'left');
			$this->db->join('dept', 'dept.dept_id = users.dept_id', 'left');
			
			if(@$searchParams['user_id']){	
				$user_arr = explode(',',substr($searchParams['user_id'],0,-1));
				$this->db->where_in('worklog.user_id', $user_arr);
			}
			if(@$searchParams['role_id'])
				$this->db->where('role.role_id', $searchParams['role_id']);
			if(@$searchParams['wids'])
				$this->db->where('log_date >=', $searchParams['wids']);
			if(@$searchParams['wide'])
				$this->db->where('log_date <=', $searchParams['wide']);
			
			//$this->db->order_by('dept.dept_id asc, role.role_id asc, worklog.user_id asc'); 
			$query=$this->db->get();
			return $query->num_rows();
		}

	}
	function worklog_manage_limit($start,$end,$sort,$order,$searchParams = ''){	
		if($searchParams != ''){
			$this->db->select('dept.dept_name,role.role_name,worklog.user_id,worklog.user_name,worklog.log_date,worklog.log_w,worklog.log_am,worklog.log_pm,worklog.log_morn,worklog.id');
			$this->db->from('worklog');
			$this->db->join('users', 'worklog.user_id = users.user_id', 'left');
			$this->db->join('role', 'role.role_id = users.role_id', 'left');
			$this->db->join('dept', 'dept.dept_id = users.dept_id', 'left');
	
			if(@$searchParams['user_id']){
				$user_arr = explode(',',substr($searchParams['user_id'],0,-1));
				$this->db->where_in('worklog.user_id', $user_arr);
			}
			if(@$searchParams['role_id'])
				$this->db->where('role.role_id', $searchParams['role_id']);
			if(@$searchParams['wids'])
				$this->db->where('log_date >=', $searchParams['wids']);
			if(@$searchParams['wide'])
				$this->db->where('log_date <=', $searchParams['wide']);
					
			$this->db->order_by('dept.dept_id asc, role.role_id asc, worklog.user_id asc'); 
			//$this->db->order_by("$sort", $order); 
			$this->db->limit($end,$start);
			$query=$this->db->get();
			//echo $this->db->last_query();//输出随后一次查询的语句
			return $query->result();
		}
	}
	*/
	
	//导出查询的数据
	function worklog_table($data){	
		if(!empty($data['user_id']) || !empty($data['role_id']) || !empty($data['wids']) || !empty($data['wide'])){
			$this->db->select('dept.dept_name,role.role_name,worklog.user_id,worklog.user_name,worklog.log_date,worklog.log_w,worklog.log_am,worklog.log_pm,worklog.log_morn,worklog.id');
			$this->db->from('worklog');
			$this->db->join('users', 'worklog.user_id = users.user_id', 'left');
			$this->db->join('role', 'role.role_id = users.role_id', 'left');
			$this->db->join('dept', 'dept.dept_id = users.dept_id', 'left');
			
			if(array_key_exists("user_id", $data)){
				$user_arr = explode(',',reduce_multiples($data['user_id'],',',true));
				$this->db->where_in('worklog.user_id', $user_arr);
			}
			if(array_key_exists("role_id",$data))
				$this->db->where('role.role_id', $data['role_id']);
			if(array_key_exists("wids",$data))
				$this->db->where('log_date >=', $data['wids']);
			if(array_key_exists("wide",$data)){
				$this->db->where('log_date <=', $data['wide']);
			}
			
			$this->db->order_by('dept.dept_id asc, role.role_id asc, worklog.user_id asc'); 
			$query = $this->db->get();
			//echo $this->db->last_query();//输出随后一次查询的语
			return $query->result();
		}
	}
	
	/*删除工作日志方法*/
	function del_worklog($data){
		$this->db->trans_begin();//开始事务处理
		foreach($data['getid'] as $id){
			$this->db->where('id', $id)->where('user_id', $this->user_id)->delete('worklog'); 
		}
		if($this->db->trans_status() === FALSE){
			$this->db->trans_rollback();//事件回滚	
			return false;		
		}else{
			$this->db->trans_commit();//提交成功
			return true;
		}//事务结束
	}
	
	//循环出父级分类 
	function get_parent_sort($table,$parent_id=0,$t=-1){ 
	
		$t++; 
		global $temp; 
		$sql = "SELECT * FROM `$table` WHERE `parent_id`=$parent_id ORDER BY `dept_id`"; 
	
		$query = mysql_query($sql); 
		$data = array(); 
		while ($res = mysql_fetch_assoc($query)){
			$data[] = $res; 
		} 
	
		if(!empty($data)){ 
			foreach ($data as $key => $val ){ 
				//$val['dept_name'] = str_repeat('&nbsp;',$t*3).'|--'.$val['dept_name']; 
				$temp[] = $val['dept_id']; 
				$this->get_parent_sort($table,$val['dept_id'],$t); 
			} 
		} 
		return $temp; 
	} 
	//带限制的部门列表
	function deptlist_stint($dept_id=''){
		if($this->acl->check_privilege('worklog_m_0') != false || $this->acl->check_privilege('worklog_m_1') != false || $this->acl->check_privilege('worklog_m_2') != false){
			
			$this->db->select('dept_id, dept_name, dept_sort, parent_id, dept_iconCls');
			$query1 = $this->db->select('parent_id')->where('dept_id',$this->session->userdata('dept_id'))->get('dept'); 
			$deptp_id = $query1->row()->parent_id;
			if($this->acl->check_privilege('worklog_m_0') != false){
				$pid = 0;
			}else if($this->acl->check_privilege('worklog_m_1') != false){
				$query2 = $this->db->select('parent_id')->where('dept_id',$deptp_id)->get('dept'); 
				$pid = $query2->row()->parent_id;
				$dept_in = $this->get_parent_sort('yj_dept',$deptp_id);
				//$dept_in[] = $deptp_id;
				array_unshift($dept_in,$deptp_id);
				
				$this->db->where_in('dept_id',$dept_in);
			}else if($this->acl->check_privilege('worklog_m_2') != false){
				$pid = $deptp_id;
				$this->db->where('dept_id',$this->session->userdata('dept_id'));
			}
			
			$this->db->order_by("dept_sort", "asc"); 
			$query = $this->db->get('dept');
			//echo $this->db->last_query();//输出随后一次查询的语句
			foreach($query->result_array() as $dept){
				$deptarr[] = array('id' => $dept['dept_id'],'text'=>$dept['dept_name'],'parentId'=>$dept['parent_id'],'dept_sort'=>$dept['dept_sort'],'dept_iconCls'=>$dept['dept_iconCls']);
			}
			$resetarr['data'] = $deptarr;
			$resetarr['p_id'] = $pid;
			return $resetarr;
		}else{
			show_error("您没有权限访问!");
			return ;
		}	
	}
}
?>